package com.gzsxy.esjy.redis.extend.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gzsxy.esjy.common.base.exception.impl.CustomAssert;
import com.gzsxy.esjy.redis.extend.RedisService;
import com.gzsxy.esjy.redis.extend.StringRedisTemplate;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 默认redis实现
 * @date 2021/10/18 17:15
 */
public class DefaultRedisServiceImpl implements RedisService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void setString(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void setString(String key, String value, long time) {
        redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }

    @Override
    public String getString(String key) {
        return key == null ? null : (String) redisTemplate.opsForValue().get(key);
    }

    @Override
    public void setValue(String key, Object value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void setValue(String key, Object value, long time) {
        redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }

    @Override
    public Object getValue(String key) {
        return key == null ? null : redisTemplate.opsForValue().get(key);
    }

    @Override
    public <T> T getValue(String key, Class<T> clazz) {
        if (key == null) {
            return null;
        }
        Object object = redisTemplate.opsForValue().get(key);
        CustomAssert.notTrue(!(object instanceof JSONArray), "值类型为List");
        if (object instanceof JSONObject) {
            return ((JSONObject) object).toJavaObject(clazz);
        }
        return (T) object;

    }

    ///
    @Override
    public Object getObject(String key, Class<?> clazz) {
        Object object = redisTemplate.opsForValue().get(key);
        if (object == null){
            return  null;
        }
        if (object instanceof  JSONObject){
            return ((JSONObject)object).toJavaObject(clazz);
        }
        return ((JSONArray)object).toJavaList(clazz);
    }

    @Override
    public boolean expire(String key, long expire) {
        if (expire <= 0 ){
            return false;
        }
        return redisTemplate.expire(key,expire,TimeUnit.SECONDS);
    }

    @Override
    public void remove(String key) {
        redisTemplate.delete(key);
    }

    @Override
    public void put(String key, String hashKey, Object value) {
        redisTemplate.opsForHash().put(key,hashKey,value);
    }

    @Override
    public void putAll(String key, Map<String, Object> map) {
        redisTemplate.opsForHash().putAll(key,map);

    }

    @Override
    public void putDto(String key, Object dto) {


    }

    @Override
    public Long deleteHashKeys(String key, String... hashKeys) {
        return redisTemplate.opsForHash().delete(key,hashKeys);
    }

    @Override
    public Object get(String key, String hashKey) {
        return redisTemplate.opsForHash().get(key,hashKey);
    }

    @Override
    public <R> R getDto(String key, Class<R> r) {
        return null;
    }

    @Override
    public Map<String, Object> getMap(String key) {
        return redisTemplate.opsForHash().entries(key);
    }

    @Override
    public Set<String> hashKeys(String key) {
        return redisTemplate.opsForHash().keys(key);
    }

    @Override
    public List<Object> hashValues(String key) {
        return redisTemplate.opsForHash().values(key);
    }

    @Override
    public boolean getLock(String key, String unLockId, long time) {
        return redisTemplate.setNx(key, unLockId, time);
    }

    @Override
    public void unLock(String key, String unLockId) {
        redisTemplate.removeNx(key, unLockId);
    }

    @Override
    public long incr(String key, long delta) {
        CustomAssert.notTrue(delta>0,"递增因子必须大于0");
        return redisTemplate.opsForValue().increment(key,delta);
    }

    @Override
    public long decr(String key, long delta) {
        CustomAssert.notTrue(delta>0,"递减因子必须大于0");
        return redisTemplate.opsForValue().increment(key,-delta);
    }

    @Override
    public double hincr(String key, String item, double by) {
        return redisTemplate.opsForHash().increment(key,item,by);
    }

    @Override
    public double hdecr(String key, String item, double by) {
        return redisTemplate.opsForHash().increment(key, item, -by);
    }

    @Override
    public Set<Object> sGet(String key) {
        try {
            return redisTemplate.opsForSet().members(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public boolean sHasKey(String key, Object value) {
        try {
            return redisTemplate.opsForSet().isMember(key, value);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public long sSet(String key, Object[] values) {
        try {
            return redisTemplate.opsForSet().add(key,values);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public long sGetSetSize(String key) {
        try {
            return redisTemplate.opsForSet().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public long setRemove(String key, Object... values) {
        return redisTemplate.opsForSet().remove(key,values);
    }

    @Override
    public long IRemove(String key, long count, Object value) {
        try {
            Long remove = redisTemplate.opsForList().remove(key, count, value);
            return remove;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public boolean IUpdateIndex(String key, long index, Object value) {
        try {
            redisTemplate.opsForList().set(key, index, value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public boolean ISet(String key, List<Object> value) {
        try {
            redisTemplate.opsForList().rightPushAll(key,value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public boolean ISet(String key, Object value) {
        try {
          redisTemplate.opsForList().rightPush(key,value);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public Object IGetIndex(String key, long index) {
        try {
            return redisTemplate.opsForList().index(key, index);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public long IGetListSize(String key) {
        try {
            return redisTemplate.opsForList().size(key);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

    }

    @Override
    public List<Object> lGet(String key, long start, long end) {
        try {
            return redisTemplate.opsForList().range(key,start,end);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public <T> List<T> getList(String key, Class<T> clazz) {
        if (key == null){
            return null;
        }
        Object object = redisTemplate.opsForValue().get(key);
        if (object == null){
            return null;
        }
        CustomAssert.notTrue(object instanceof JSONArray,"值不为List");
        return ((JSONArray)object).toJavaList(clazz);
    }

    @Override
    public boolean exists(String key) {
        return redisTemplate.hasKey(key);
    }
}
